﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;
namespace Chizx.Web.user
{
    public partial class List : Page
    {


        Chizx.BLL.UserBll bll = new Chizx.BLL.UserBll();
        Chizx.BLL.View.zx_view_user vbll = new Chizx.BLL.View.zx_view_user();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["UserEntity"])
            {
                Response.Redirect("/e/Login.aspx");
            }
            if (!Page.IsPostBack)
            {
                btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？删除之后与之关联的实习申请,实习总结,系统日志数据亦将随之删除！\")");
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bll.DeleteList(idlist);
            BindData();
        }

        #region gridView

        public void BindData()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            string scoption1 = scOption1.SelectedItem.Value;
            string scoption2 = scOption2.SelectedItem.Value;
            string scoption = scoption1;
            if (Request.QueryString["param"] == "PS")
            {//普通用户
                strWhere.AppendFormat("zu_roles like '%{0}%'", "S");
                scoption = scoption1;
                scOption2.Enabled = false;
                scOption2.Visible = false;
            }
            else if (Request.QueryString["param"] == "PA")
            {//管理员用户
                strWhere.AppendFormat("zu_roles like '%{0}%'", "A");
                scoption = scoption2;
                scOption1.Enabled = false;
                scOption1.Visible = false;
                this.gridView.Columns[1].HeaderText = "教职工号";
                this.gridView.Columns[4].Visible = false;
                this.gridView.Columns[5].Visible = false;
                this.gridView.Columns[6].Visible = false;
                this.gridView.Columns[7].Visible = false;
                this.gridView.Columns[10].Visible = false;
            }
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat("  and " + scoption + " like '%{0}%'", txtKeyword.Text.Trim());
            }
            ds = vbll.GetList(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？删除之后与之关联的实习申请,实习总结,系统日志数据亦将随之删除！\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}

            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string ID = (string)gridView.DataKeys[e.RowIndex].Value;
            bll.Delete(ID);
            BindData();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion





    }
}
